package com.zhuangfei.hpuhelper.view;

import java.util.ArrayList;
import java.util.List;





import cn.lzf.serviceplatform.R;

import com.hpu.bean.Course;
import com.hpu.constants.HttpConstants;
import com.hpu.constants.ShareConstants;
import com.hpu.service.ShareDownloadService;
import com.hpu.tools.ShareTools;
import com.zhuangfei.hpuhelper.tools.ActivityTools;
import com.zhuangfei.hpuhelper.tools.CourseTools;
import com.zhuangfei.hpuhelper.tools.DefaultHandler;
import com.zhuangfei.hpuhelper.view.AddCourseActivity;
import com.zhuangfei.hpuhelper.view.CourseManagerActivity;
import com.zhuangfei.hpuhelper.view.MainActivity;
import com.zhuangfei.smartalert.core.ImageAlert;
import com.zhuangfei.smartalert.core.LoadAlert;
import com.zhuangfei.smartalert.core.MessageAlert;
import com.zhuangfei.smartalert.listener.OnMessageAlertAdapter;
import com.zhuangfei.toolkit.tools.ToastTools;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class ImportCourseActivity extends Activity implements OnClickListener{

	private LinearLayout courseContainerLayout;
	private LinearLayout backLayout;
	
	private LinearLayout allCheckLayout;
	private LinearLayout importLayout;
	private CheckBox allCheckBox;
	
	List<CheckBox> boxs = new ArrayList<>();
	List<Course> courses = new ArrayList<>();
	List<Course> courseList = new ArrayList<>();
	private String term;
	private String id,course;
	private ShareDownloadService shareDownloadService;
	private LoadAlert loadAlert;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_import_course);
//		ActivityTools.setTransparent(this);
		id=getIntent().getStringExtra("id");
		initView();
		initEvent();
		shareDownloadService.doShareDownload(id);
		loadAlert.show();
	}
	
	
	private void initEvent() {
		backLayout.setOnClickListener(this);
		allCheckLayout.setOnClickListener(this);
		importLayout.setOnClickListener(this);
	}

	private void initView() {
		courseContainerLayout = (LinearLayout) findViewById(R.id.id_course_import_container);
		backLayout = (LinearLayout) findViewById(R.id.id_back);
		allCheckLayout = (LinearLayout) findViewById(R.id.id_import_allcheck);
		importLayout = (LinearLayout) findViewById(R.id.id_import_doimport);
		allCheckBox = (CheckBox) findViewById(R.id.id_box_allcheck);
		
		shareDownloadService=new ShareDownloadService(handler, this);
		loadAlert=new LoadAlert(this).create();
	}

	DefaultHandler handler=new DefaultHandler(this){
		public void handleMessage(android.os.Message msg) {
			loadAlert.hide();
			super.handleMessage(msg);

			switch (msg.what) {
			case HttpConstants.SHARE_COURSE_DOWNLOAD_SUCCESS:
				course=msg.obj.toString();
				addCourseItemView();
				break;

			default:
				break;
			}
		};
	};
	private void addCourseItemView() {
		courseContainerLayout.removeAllViews();
		term = (String) ShareTools.get(this, ShareConstants.STORE_TERM, "default-term");
		if(term.equals("default-term")){
			ToastTools.show(this,"你的学期未获取，本次添加的课程所属默认学期:default-term");
		}
		courseList = CourseTools.getCourseFrom(course);
		LinearLayout containerLayout;
		TextView nameTextView, roomTextView, weeksTextView, dayTextView, teacherTextView;

		for (int i = 0; i < courseList.size(); i++) {
			final Course course = courseList.get(i);
			final View view = LayoutInflater.from(this).inflate(R.layout.item_coursemanager_layout, null);
			containerLayout = (LinearLayout) view.findViewById(R.id.id_container);
			nameTextView = (TextView) view.findViewById(R.id.id_manager_name);
			roomTextView = (TextView) view.findViewById(R.id.id_manager_room);
			weeksTextView = (TextView) view.findViewById(R.id.id_manager_weeks);
			dayTextView = (TextView) view.findViewById(R.id.id_manager_day);
			teacherTextView = (TextView) view.findViewById(R.id.id_manager_teacher);
			
			final CheckBox checkBox = (CheckBox) view.findViewById(R.id.id_manager_check);
			

			boxs.add(checkBox);
			
			containerLayout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					if (checkBox.isChecked()) {
						checkBox.setChecked(false);
						courses.remove(course);
					} else {
						checkBox.setChecked(true);
						courses.add(course);
					}
					
					if(courseList.size()!=0&&courseList.size()==courses.size()){
						allCheckBox.setChecked(true);
					}else{
						allCheckBox.setChecked(false);
					}
				}
			});
			

			nameTextView.setText(course.getName());
			roomTextView.setText("教室\t" + course.getRoom());
			String s = "";
			List<Integer> weekList = course.getWeekList();
			for (int j = 0; j < weekList.size(); j++) {
				s += weekList.get(j) + ",";
			}

			weeksTextView.setText("周次\t" + s);
			dayTextView.setText("节次\t周" + getDay(course.getDay()) + ",\t" + course.getStart() + "-"
					+ (course.getStart() + course.getStep() - 1) + "节");
			teacherTextView.setText("教师\t" + course.getTeacher());

			courseContainerLayout.addView(view);
		}
	}

	public String getDay(int day) {
		String str = "一二三四五六七";
		return str.charAt(day - 1) + "";
	}
	
	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.id_back:
			ActivityTools.toBackActivityAnim(ImportCourseActivity.this, MenuActivity.class);
			break;
			
		case R.id.id_import_allcheck:
			allCheck();
			break;
		
		case R.id.id_import_doimport:
			if(courses.size()==0) Toast.makeText(this, "没有选中的课程",Toast.LENGTH_SHORT).show();
			else{
			allImport();
			}
			break;
		default:
			break;
		}
	}

	private void allCheck() {
		if (allCheckBox.isChecked()) {
			for (int i = 0; i < boxs.size(); i++) {
				CheckBox checkBox = boxs.get(i);
				if (checkBox.isChecked()) {
					checkBox.setChecked(false);
					courses.remove(courseList.get(i));
				}
			}
			allCheckBox.setChecked(false);
		} else {
			for (int i = 0; i < boxs.size(); i++) {
				CheckBox checkBox = boxs.get(i);
				if (!checkBox.isChecked()) {
					checkBox.setChecked(true);
					courses.add(courseList.get(i));
				}
			}
			allCheckBox.setChecked(true);
		}
	}
	
	private void allImport() {
		CourseTools.appendCourse(this, courses, term);
		Toast.makeText(this,"课程已导入!",Toast.LENGTH_SHORT).show();
		ActivityTools.toBackActivityAnim(this,MainActivity.class);
	}
	
	@Override
	public void onBackPressed() {
		ActivityTools.toBackActivityAnim(ImportCourseActivity.this, MenuActivity.class);
	}

	@Override
	protected void onDestroy() {
		if(loadAlert!=null) loadAlert.dimiss();
		super.onDestroy();
	}
}
